package com.heima.kafka.controller;

import com.alibaba.fastjson.JSON;
import com.heima.kafka.pojos.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: xrl
 * @Date: 2022/10/18  20:38
 * @Version 1.0
 * 消息生产者
 */

@RestController
public class HelloController {

    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    @GetMapping("/hello")
    public String hello() {
        kafkaTemplate.send("itcast-topic", "java大世界");
        return "ok";
    }

    @GetMapping("/student")
    public String student() {
        Student stu = new Student();
        stu.setName("小明");
        stu.setAge(22);
        stu.setMoney(8000);
        kafkaTemplate.send("student-topic", JSON.toJSONString(stu));
        return "ok";
    }
}
